﻿using LS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace LS.ModelLogicHelpers
{
    public class CardAdminLogicHelper
    {
        private LSDbContext db = new LSDbContext();
        private static char[] constant =   
        {   
        '0','1','2','3','4','5','6','7','8','9',   
        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',   
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'   
        };
        public static string CrearNewSeiralNumber()
        {
            StringBuilder rtn = new StringBuilder();
            rtn.Append(GenerateRandom(constant, 9));
            rtn.Insert(3, '-');
            rtn.Insert(7, '-');
            return rtn.ToString();
        }

        public static string GenerateRandom(char[] source, int resultLength)
        {
            StringBuilder newRandom = new StringBuilder(resultLength);
            Random rd = new Random();
            for (int i = 0; i < resultLength; i++)
            {
                var Guidseed = Math.Abs((Guid.NewGuid().GetHashCode()));
                var ticks = DateTime.Now.Ticks%Guidseed;
                var randomSeed = rd.Next(Guidseed);
                var index = Math.Abs((ticks * randomSeed * Guidseed) % source.Length);
                newRandom.Append(source[index]);
            }
            return newRandom.ToString();
        }
        //private static int RandomAlgorithm(List<long> seeds)
        //{
        //    List<Action<double>> d = new List<Action<double>>();
        //}
        public CardManageModel CreateNewCard()
        {
            CardManageModel rtn = new CardManageModel();
            rtn.ExpireDate = DateTime.Now.AddYears(1);
            rtn.Card = new Card();
            rtn.Card.SeiralNumber = CardAdminLogicHelper.CrearNewSeiralNumber();
            return rtn;
        }


    }
}